package com.example.onlineeducation.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.onlineeducation.base.BaseViewModel
import com.example.onlineeducation.data.entity.*
import com.example.onlineeducation.data.json.HotSearchJson
import com.example.onlineeducation.repository.MainHomeRepository
import com.example.onlineeducation.repository.MainTopicRepository
import com.example.onlineeducation.repository.SearchRepository
import com.example.onlineeducation.repository.StudyRepository
import com.yc.jetpacklib.net.YcResult
import com.yc.jetpacklib.net.doFail
import com.yc.jetpacklib.net.doSuccess
import kotlinx.coroutines.flow.collectLatest

class MainHomeVM  : BaseViewModel() {
    private val mMainHomeRepository: MainHomeRepository by lazy { MainHomeRepository(mApiService) }
    private val mStudyRepository: StudyRepository by lazy { StudyRepository(mApiService) }
    private val mSearchRepository: SearchRepository by lazy { SearchRepository(mApiService) }
    private val mMainTopicRepository: MainTopicRepository by lazy { MainTopicRepository(mApiService) }

    private val _mBannerListDataModel = MutableLiveData<YcResult<List<BannerEntity>?>>()
    val mBannerListDataModel: LiveData<YcResult<List<BannerEntity>?>> = _mBannerListDataModel
    fun getBanner(projectId :String) = ycLaunch {
        mMainHomeRepository.getBanner(projectId).ycHasLoading().collectLatest {
            _mBannerListDataModel.postValue(it)
        }
    }


    private val _mNoticeListDataModel = MutableLiveData<YcResult<List<NoticeEntity>?>>()
    val mNoticeListDataModel: LiveData<YcResult<List<NoticeEntity>?>> = _mNoticeListDataModel
    fun getNotice(projectId :String) = ycLaunch {
        mMainHomeRepository.getNotice(projectId).ycHasLoading().collectLatest {
            _mNoticeListDataModel.postValue(it)
        }
    }

    private val _mStudyPlanList = MutableLiveData<YcResult<List<StudyPlanEntity>?>>()
    val mStudyPlanList: LiveData<YcResult<List<StudyPlanEntity>?>> = _mStudyPlanList
    fun getStudyPlanList(projectId :String) = ycLaunch {
        mStudyRepository.getStudyPlanList(projectId).ycHasLoading().collectLatest {
            _mStudyPlanList.postValue(it)
        }
    }

    private val _mGetHotSearch = MutableLiveData<YcResult<List<HotSearchJson>?>>()
    val mGetHotSearch: LiveData<YcResult<List<HotSearchJson>?>> = _mGetHotSearch
    fun getHotSearch() = ycLaunch {
        mSearchRepository.getHotSearch().ycHasLoading().collectLatest {
            _mGetHotSearch.postValue(it)
        }
    }

    private val _mSearchEntity = MutableLiveData<String?>()
    val mSearchEntity: LiveData<String?> = _mSearchEntity
    fun search(keyName: String?) {
        _mSearchEntity.postValue(keyName)
    }

    private val _mExamInfoAndTopicInfoDataModel = MutableLiveData<YcResult<ExamAndTopicEntity>>()
    val mExamInfoAndTopicInfoDataModel: LiveData<YcResult<ExamAndTopicEntity>> = _mExamInfoAndTopicInfoDataModel
    fun getExamInfoAndTopicInfo(examId: String,topicId: String,projId: String) = ycLaunch {
        mStudyRepository.getExamInfoAndTopicInfo(examId,topicId,projId).ycHasLoading().collectLatest {
            _mExamInfoAndTopicInfoDataModel.postValue(it)
        }
    }

    private val _mCheckStudyTimeDataModel = MutableLiveData<YcResult<ExamAndTopicEntity>>()
    val mCheckStudyTimeDataModel: LiveData<YcResult<ExamAndTopicEntity>> = _mCheckStudyTimeDataModel
    fun getCheckStudyTime(projectId: String, topicId: String, mExamEntity: ExamAndTopicEntity) = ycLaunch {
        mMainTopicRepository.getCheckStudyTime(projectId, topicId).collectLatest {
            it.doSuccess {
                _mCheckStudyTimeDataModel.postValue(
                    YcResult.Success(mExamEntity)
                )
            }.doFail {
                _mCheckStudyTimeDataModel.postValue(
                    YcResult.Fail(it)
                )
            }
        }
    }
}
